function [] = savedata(save_data, save_type);

% SAVEDATA - save data array to a file or workspace variable
%
%    SAVEDATA(DATA, 'txt') exports the data array to a tab-
%    delimited ASCII text file.  DATA must be a 2D numeric
%    array.
%
%    SAVEDATA(DATA, 'mat') exports the data array to a MATLAB
%    MAT file.  DATA can be any MATLAB array.
%
%    SAVEDATA(DATA, 'var') exports the data array to a workspace
%    variable.  DATA can be any MATLAB array.
%
%    SAVEDATA(DATA, 'log') exports the data array to a log file.
%    DATA must be a cell array of strings.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: August 20, 2000

% global declarations

global DATA_PATH				% data file path

% default parameters

mat_filter = '*.mat';		% file filter for MAT files
txt_filter = '*.txt';		% file filter for TXT files
txt_delimiter = '\t';		% delimiting character for TXT files

% validate arguments

if (nargin ~= 2)
   msgbox('Invalid number of arguments', 'SAVEDATA Error', 'warn');
   return
elseif (~ischar(save_type) | isempty(save_type))
   msgbox('TYPE must be a character string', 'SAVEDATA Error', 'warn');
   return
elseif (strcmp(save_type, 'txt'))
   if (~isnumeric(save_data) | isempty(save_data)) 
      msgbox('DATA must be a numeric array', 'SAVEDATA Error', 'warn');
      return
   elseif (length(size(save_data)) ~= 2)
      msgbox('DATA must be a 2D array', 'SAVEDATA Error', 'warn');
      return
   end
elseif (strcmp(save_type, 'log'))
   if (~iscell(save_data) | isempty(save_data)) 
      msgbox('DATA must be a cell array', 'SAVEDATA Error', 'warn');
      return
   elseif (~all(cellfun('isclass', save_data, 'char')))
      msgbox('DATA must be a cell array of strings', 'SAVEDATA Error', 'warn');
      return
   end
end

% change to specified path if valid

if (ischar(DATA_PATH) & ~strcmp(save_type, 'var'))
   if (exist(DATA_PATH) == 7)
      cd(DATA_PATH);
   end
end

% export data to file or workspace

switch (save_type)
   
case 'txt'		% export data to delimited ASCII file
   [data_name, temp_path] = uiputfile(txt_filter, 'Export Array to Text File');
   if ((data_name == 0) & (temp_path == 0))
      return
   else
      DATA_PATH = temp_path;
   end
   data_fid = fopen([DATA_PATH data_name], 'wt');
   if (data_fid == -1)
      msgbox(['Cannot open ' DATA_PATH data_name '.'], 'SAVEDATA Error', 'warn');
      return
   end
   for i = 1 : size(save_data, 1)
      data_line = sprintf('%g', double(save_data(i, 1)));
      if (size(save_data, 2) > 1)
         data_line = [data_line sprintf([txt_delimiter '%g'], double(save_data(i, 2 : end)))];
      end
      fprintf(data_fid, '%s\n', data_line);
   end
   status = fclose(data_fid);
   
case 'mat'		% export data to MATLAB MAT file
   [data_name, temp_path] = uiputfile(mat_filter, 'Export Array to Matlab File');
   if ((data_name == 0) & (temp_path == 0))
      return
   else
      DATA_PATH = temp_path;
   end
   cd(DATA_PATH);
   eval(['save ' data_name ' save_data'], '');
   
case 'var'		% export data to workspace variable
   save_name = inputdlg({'variable name = '}, 'Export Array to Workspace', 1);
   if (~isempty(save_name))
      if (ischar(save_name{1}) & ~isempty(save_name{1}))
         exist_flag = evalin('base', ['exist(''' save_name{1} ''')'], '0');
         if (exist_flag == 1)
            button = questdlg([save_name{1} ' already exists.  Overwrite?'], 'Overwrite Variable', 'Yes', 'No', 'No');
            if (strcmp(button, 'No'))
               return
            end
         end
         assignin('base', save_name{1}, save_data);
      end
   end
   
case 'log'		% export cell array to ASCII file
   [data_name, temp_path] = uiputfile(txt_filter, 'Export List to Text File');
   if ((data_name == 0) & (temp_path == 0))
      return
   else
      DATA_PATH = temp_path;
   end
   data_fid = fopen([DATA_PATH data_name], 'wt');
   if (data_fid == -1)
      msgbox(['Cannot open ' DATA_PATH data_name '.'], 'SAVEDATA Error', 'warn');
      return
   end
   for i = 1 : length(save_data)
      fprintf(data_fid, '%s\n', save_data{i});
   end
   status = fclose(data_fid);
   
end
return
